package com.sicau.smarttraceplatform.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sicau.smarttraceplatform.common.constants.CommonURLConstants;
import com.sicau.smarttraceplatform.common.returning.ResultData;
import com.sicau.smarttraceplatform.entity.Area;
import com.sicau.smarttraceplatform.entity.param.area.AreaQueryParam;
import com.sicau.smarttraceplatform.service.AreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.dozer.Mapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 地区 前端控制器
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2020-05-24
 */
@RestController
@RequestMapping(CommonURLConstants.AREA_PREFIX)
@Api(tags = "地区")
public class AreaController {
    @Resource
    private Mapper dozerMapper;
    @Resource
    private AreaService areaService;

    @GetMapping({"/province","/h5/province"})
    @ApiOperation("查询省")
    public ResultData getProvince() {
        List<Area> areaList = areaService.list(new QueryWrapper<Area>().select("distinct province"));
        List<String> list = new LinkedList<>();
        areaList.forEach((item)-> list.add(item.getProvince()));
        return new ResultData<>().OK().setObj(list);
    }

    @GetMapping({"/city","/h5/city"})
    @ApiOperation("查询市")
    public ResultData getCity(@ApiParam(name = "province", value = "省") @RequestParam String province) {
        List<Area> areaList = areaService.list(new QueryWrapper<Area>().select("distinct city").eq("province", province));
        List<String> list = new LinkedList<>();
        areaList.forEach((item)-> list.add(item.getCity()));
        return new ResultData<>().OK().setObj(list);
    }

    @GetMapping({"/district","/h5/district"})
    @ApiOperation("查询区县")
    public ResultData getDistrict(@ApiParam(name = "city", value = "市") @RequestParam String city) {
        List<Area> areaList = areaService.list(new QueryWrapper<Area>().select("distinct district").eq("city", city));
        List<String> list = new LinkedList<>();
        areaList.forEach((item)-> list.add(item.getDistrict()));
        return new ResultData<>().OK().setObj(list);
    }

    @GetMapping({"","/h5"})
    @ApiOperation("根据id查询")
    public ResultData getById(@RequestParam String id){
        Area area = areaService.getById(id);
        return new ResultData<>().OK().setObj(area);
    }

    @GetMapping({"/specific","/h5/specific"})
    @ApiOperation("根据id查询")
    public ResultData getSpecific(@Valid AreaQueryParam areaQueryParam){
        List<Area> areaList = areaService.queryByCondition(areaQueryParam);
        return new ResultData<>().OK().setObj(areaList);
    }


}

